/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.raflik.queryxml.ui.model;

import java.lang.reflect.InvocationTargetException;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.nodes.PropertySupport;
import org.openide.nodes.Sheet;
import org.openide.util.NbBundle;
import pl.raflik.queryxml.lib.ResultType;
import pl.raflik.queryxml.lib.XMLNodePresenter;

/**
 *
 * @author raflik
 */
public class ResultNode extends AbstractNode {

    private class NameProperty extends PropertySupport.ReadOnly<String> {

        public NameProperty() {
            super("NameProperty", String.class, NbBundle.getMessage(ResultNode.class, "ResultNode.Columns.Name"), "");
        }

        @Override
        public String getValue() throws IllegalAccessException, InvocationTargetException {
            if (obj != null) {
                if (obj instanceof org.w3c.dom.Node) {
                    org.w3c.dom.Node node = (org.w3c.dom.Node) obj;
                    return XMLNodePresenter.getNodeName(node);
                }
                return obj.getClass().getSimpleName();
            }
            return null;
        }
    }

    private class ValueProperty extends PropertySupport.ReadOnly<String> {

        public ValueProperty() {
            super("ValueProperty", String.class, NbBundle.getMessage(ResultNode.class, "ResultNode.Columns.Value"), "");
        }

        @Override
        public String getValue() throws IllegalAccessException, InvocationTargetException {
            if (obj != null) {
                if (obj instanceof org.w3c.dom.Node) {
                    org.w3c.dom.Node node = (org.w3c.dom.Node) obj;
                    return XMLNodePresenter.getNodeValue(node);
                }
                return obj.toString();
            }
            return null;
        }
    }
    private Object obj;

    public ResultNode() {
        super(Children.LEAF);
    }

    public ResultNode(Object obj) {
        super(ResultChildren.createChildren(obj));
        this.obj = obj;
        String displayName = ResultType.formatType(obj.getClass());
        setDisplayName(displayName);
    }

    @Override
    protected Sheet createSheet() {
        Sheet s = super.createSheet();
        Sheet.Set ss = s.get(Sheet.PROPERTIES);
        if (ss == null) {
            ss = Sheet.createPropertiesSet();
            s.put(ss);
        }
        ss.put(new NameProperty());
        ss.put(new ValueProperty());
        return s;
    }
}
